package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.data.entity.ResultInfoCre;
import com.kbbywyclient.data.model.BoughtTableCreditModel;
import com.kbbywyclient.data.model.CreditCardTableMenuModel;
import com.kbbywyclient.data.model.StatusUpdateModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.BoughtTableCreditContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;

/**
 * Description:
 * Created by FHW
 * on 2017/4/17
 */

public class BoughtTableCrePresenter extends BasePresenter implements BoughtTableCreditContract.Presenter {

    private BoughtTableCreditContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private int pageno = 1;
    private List<ResultInfoCre> resultInfoCreList = null;

    @Inject
    public BoughtTableCrePresenter(BoughtTableCreditContract.View view) {
        this.view = view;
        resultInfoCreList = new ArrayList<>();
        this.view.setAdapterOrNotify(resultInfoCreList);
    }

    @Override
    public void getCreditCardBuyOrders(boolean isClean, String SalesID, String Dist, String Education, String UnitNature, String CreditCard) {
        if (isClean) {
            pageno = 1;
        }
        rsaJsonUtil.put("pageno", pageno + "");
        rsaJsonUtil.put("pageSize", API.PAGE_SIZE);

        rsaJsonUtil.put("SalesID", SalesID);
        if (StringUtil.isNotEmpty(Dist)) {
            rsaJsonUtil.put("Dist", Dist);
        }
        if (!Education.equals("0")) {
            rsaJsonUtil.put("Education", Education);
        }
        if (StringUtil.isNotEmpty(UnitNature)) {
            rsaJsonUtil.put("UnitNature", UnitNature);
        }
        if (StringUtil.isNotEmpty(CreditCard)) {
            rsaJsonUtil.put("CreditCard", CreditCard);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getCreditCardBuyOrders(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<BoughtTableCreditModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.loadFaiBoughtTableCre(e.toString());
                    }

                    @Override
                    public void onNext(BoughtTableCreditModel boughtTableCreditModel) {
                        if (isClean) {
                            resultInfoCreList.clear();
                        }
                        resultInfoCreList.addAll(boughtTableCreditModel.getData().getResult());
                        view.setAdapterOrNotify(resultInfoCreList);//刷新
                        view.loadSucBoughtTableCre(resultInfoCreList);
                        //分页
                        if (boughtTableCreditModel.getData().getPaging().getCurrentPage() >= boughtTableCreditModel.getData().getPaging().getTotalPage()) {
                            if (boughtTableCreditModel.getData().getResult().size() > 1) {
                                view.setNoMore();
                            }
                        } else {
                            pageno++;
                            view.setMoreComplete();
                        }
                    }
                });
    }

    @Override
    public void getCreditCardMenu(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
        ApiServiceHelper.
                appointThreadNormal(apiService.getCreditCardMenu(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<CreditCardTableMenuModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(CreditCardTableMenuModel creditCardTableMenuModel) {
                        view.loadSucMenu(creditCardTableMenuModel.getData());
                    }
                });
    }

    @Override
    public void getapplyCreditCardUpdate(String SalesID, String ApplyCreditCardID, String ApplyCreditCardSN, String Status, String SuccessDescription) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("ApplyCreditCardID", ApplyCreditCardID);
        rsaJsonUtil.put("ApplyCreditCardSN", ApplyCreditCardSN);
        if (StringUtil.isNotEmpty(Status)) {
            rsaJsonUtil.put("Status", Status);
        }
        if (StringUtil.isNotEmpty(SuccessDescription)) {
            rsaJsonUtil.put("SuccessDescription", SuccessDescription);
        }
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getApplyCreditCardUpdate(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<StatusUpdateModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(StatusUpdateModel statusUpdateModel) {
                        view.loadSucCreditCardUpdate(statusUpdateModel.getData());
                    }
                });


    }

}
